package cn.e4j.modular.api.service;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.mapper.Gwat310Mapper;
import cn.e4j.modular.api.mapper.Gwat600Mapper;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.crm.entity.*;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

/**
 * 安装信息回报
 */
@Service
public class Gwat600Service{

    @Autowired
    Gwat600Mapper gwat600Mapper;

    @Autowired
    Gwat310Mapper gwat310Mapper;
    /**
     * 获取列表信息
     *
     * @param inDto
     * @return
     */
    public List<Dto> getGwcvData(Page<Object> page,Dto inDto) {
        inDto.put("GWCSENT", inDto.getString("ENT"));
        inDto.put("LANG", "2");
        List<Dto> dtoList = gwat600Mapper.getGwcvPage(page,inDto);
        for (Dto dto : dtoList) {
            inDto.put("GWCS010", dto.getString("GWCS010"));
            inDto.put("GWCS008", dto.getString("GWCS008"));
            inDto.put("GWCS001", dto.getString("GWCS001"));
            inDto.put("GWCS019", dto.getString("GWCS019"));
            inDto.put("GWCVDOCNO", dto.getString("GWCVDOCNO"));
            List<Dto> list = gwat600Mapper.getGwcvDetail(inDto);
            dto.put("list", list);
        }
        return dtoList;
    }

    /**
     * 回报
     *
     * @param
     */
    @Transactional
    public Dto confirm(SysUser user,JSONObject inDto) {
        Dto outDto = new Dto();
        Date date = new Date();
        LocalDate now = LocalDate.now();
//        List<Dto> listDto = new ArrayList<Dto>();
//        Map<String,Object> map = new HashMap<>();
        if ("single".equals(inDto.getString("type"))) {
//            inDto = AppJson.fromJson(inDto.getString("item"), new TypeToken<HashDto>() {
//            }.getType());
            inDto = JSONObject.parseObject(inDto.getString("item"));
        }
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(inDto.get("list")));

//        listDto =(List<Dto>)JSONObject.parse());
        //设备安装信息表部分
        String docno = "DDAZ" + DateUtils.getDate("yyyyMMddHHmmssSSS");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonobj =jsonArray.getJSONObject(i);
            GwcvT gwcv_tPO = new GwcvT();
            gwcv_tPO.setGWCVENT(user.getDBC());
            gwcv_tPO.setGWCVDOCNO(docno);
            gwcv_tPO.setGWCVSEQ(BigDecimal.valueOf(i + 1));
            gwcv_tPO.setGWCVDOCDATE(now);
            gwcv_tPO.setGWCV001(jsonobj.getString("GWCS001"));
            gwcv_tPO.setGWCV002(jsonobj.getString("GWCS019")==null?" ":jsonobj.getString("GWCS019"));
            gwcv_tPO.setGWCV003(jsonobj.getString("GWCS002"));
            gwcv_tPO.setGWCV004(jsonobj.getBigDecimal("GWCS003"));
            gwcv_tPO.setGWCV006(inDto.getString("GWCV006"));
            SysUser sys_userPO = new SysUser().setID(gwcv_tPO.getGWCV006()).selectById();
//            SysUser sys_userPO = sys_userDao.selectByKey(gwcv_tPO.getGWCV006());
            if (sys_userPO != null) {
                gwcv_tPO.setGWCV005(sys_userPO.getORG_ID());
                gwcv_tPO.setGWCV007(sys_userPO.getMOBILE());
            }
            /*gwcv_tPO.setGWCV008(dto.getDate("GWCS016"));
            gwcv_tPO.setGWCV009(inDto.getDate("GWCV009"));*/
            gwcv_tPO.setGWCV008(CommonUtils.dateToLocalDate(jsonobj.getDate("GWCS016")));
            gwcv_tPO.setGWCV009(CommonUtils.dateToLocalDate(jsonobj.getDate("GWCV009")));
            gwcv_tPO.setGWCV010(inDto.getString("GWCV010"));
            gwcv_tPO.setGWCV030(jsonobj.getString("GWCSDOCNO"));
            gwcv_tPO.setGWCV031(jsonobj.getBigDecimal("GWCSSEQ"));
            gwcv_tPO.setGWCV050(inDto.getString("GWCV050"));
            gwcv_tPO.setGWCVACTI("Y");

            gwcv_tPO.setGWCVDATE(now);
            gwcv_tPO.setGWCVTIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));
            gwcv_tPO.setGWCVUSER(user.getID());
            gwcv_tPO.setGWCVGRUP(user.getORG_ID());
            gwcv_tPO.setGWCVORIU(user.getID());
            gwcv_tPO.setGWCVORIG(user.getORG_ID());
            gwcv_tPO.setGWCVMODU(user.getID());
            gwcv_tPO.insert();
        }
        //客户已购设备表部分
        int Mmra014 = 0;
        for (Object obj : jsonArray) {
            JSONObject mdto = JSONObject.parseObject(jsonArray.toJSONString(obj));

            Mmra014 = Mmra014 + mdto.getInteger("GWCS003");
        }
        MmraT mmraT =gwat600Mapper.getMmra(inDto);
        if (mmraT != null) {
            mmraT.setMMRA011(now);
            mmraT.setMMRAMODU(DateUtils.getDate());
            //mmraent = #{gwcsent , jdbcType = VARCHAR} AND mmra001 = #{gwcs010 , jdbcType = VARCHAR}
            //AND mmra002 = #{gwcs001 , jdbcType = VARCHAR} AND mmra003 = #{gwcs019 , jdbcType = VARCHAR}
            UpdateWrapper<MmraT> wrapper = new UpdateWrapper<>();
            wrapper.eq("MMRAENT",inDto.getString("GWCSENT"));
            wrapper.eq("MMRA001",inDto.getString("GWCS010"));
            wrapper.eq("MMRA002",inDto.getString("GWCS001"));
            wrapper.eq("MMRA003",inDto.getString("GWCS019"));
            mmraT.update(wrapper);
        } else {
            QueryWrapper<GwamT> gwamTQueryWrapper = new QueryWrapper<>();
            gwamTQueryWrapper.eq("GWAMENT", inDto.getString("GWCSENT")).
                    eq("GWAM001", inDto.getString("GWCS001"));
            GwamT gwamT = new GwamT().selectOne(gwamTQueryWrapper);

            QueryWrapper<GwaaT> gwaaTQueryWrapper = new QueryWrapper<>();
            gwaaTQueryWrapper.eq("GWAAENT", inDto.getString("GWCSENT")).
                    eq("GWAA001", inDto.getString("GWCS001"));
            GwaaT gwaaT = new GwaaT().selectOne(gwaaTQueryWrapper);


            MmraT mmra_tPO = new MmraT();
            mmra_tPO.setMMRAENT(inDto.getString("GWCSENT"));
            mmra_tPO.setMMRA001(inDto.getString("GWCS010"));
            mmra_tPO.setMMRA002(inDto.getString("GWCS001"));
            mmra_tPO.setMMRA003(inDto.getString("GWCS019")==null?" ":inDto.getString("GWCS019"));
            mmra_tPO.setMMRA004(inDto.getString("GWAML003"));
            mmra_tPO.setMMRA005(" ");//为空 待确定
            mmra_tPO.setMMRA006(inDto.getString("GWAML004"));
            mmra_tPO.setMMRA007(gwamT==null?null:gwamT.getGWAM007());
            mmra_tPO.setMMRA008(now);
            mmra_tPO.setMMRA009(" ");
            mmra_tPO.setMMRA010(now);
            mmra_tPO.setMMRA011(now);
            mmra_tPO.setMMRA013("1");
            mmra_tPO.setMMRA014(String.valueOf(Mmra014));
            mmra_tPO.setMMRA015(gwamT==null?null:gwamT.getGWAM009());
            mmra_tPO.setMMRA020(gwaaT==null?null:gwaaT.getGWAA009());
            mmra_tPO.setMMRAACTI("Y");
            mmra_tPO.setMMRADATE(now);
            mmra_tPO.setMMRATIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));
            mmra_tPO.setMMRAUSER(user.getID());
            mmra_tPO.setMMRAGRUP(user.getORG_ID());
            mmra_tPO.setMMRAORIU(user.getIDNO());
            mmra_tPO.setMMRAORIG(user.getORG_ID());
            mmra_tPO.setMMRAMODU(user.getID());

            mmra_tPO.insert();

            List<Dto> gwax_tList = gwat600Mapper.getGwax(inDto);
            for (Dto mmrbDto : gwax_tList) {
                MmrbT mmrb_tPO = new MmrbT();
                mmrb_tPO.setMMRBENT(mmra_tPO.getMMRAENT());
                mmrb_tPO.setMMRB001(mmra_tPO.getMMRA001());
                mmrb_tPO.setMMRB002(mmra_tPO.getMMRA002());
                mmrb_tPO.setMMRB003(mmra_tPO.getMMRA003());

                QueryWrapper<GwamlT> gwamlTQueryWrapper = new QueryWrapper<>();
                gwamlTQueryWrapper.eq("GWAMLENT", mmrb_tPO.getMMRBENT()).
                        eq("GWAML001", mmrbDto.getString("GWAX003")).
                        eq("GWAML002", "2");
                GwamlT gwaml_tPO_ = new GwamlT().selectOne(gwamlTQueryWrapper);
                mmrb_tPO.setMMRB004(mmrbDto.getString("GWAX003"));
                mmrb_tPO.setMMRB005(" ");
                mmrb_tPO.setMMRB006(gwaml_tPO_.getGWAML003());
                mmrb_tPO.setMMRB007(gwaml_tPO_.getGWAML004());
                mmrb_tPO.setMMRB012(mmra_tPO.getMMRA018());
                mmrb_tPO.setMMRB015(Double.parseDouble(1+""));
                mmrb_tPO.setMMRB016(" ");
                mmrb_tPO.setMMRB017(" ");
                mmrb_tPO.setMMRB024(BigDecimal.valueOf(0).doubleValue());
                mmrb_tPO.setMMRBACTI("Y");
                mmrb_tPO.setMMRBDATE(mmra_tPO.getMMRADATE());
                mmrb_tPO.setMMRBTIME(mmra_tPO.getMMRATIME());
                mmrb_tPO.setMMRBUSER(mmra_tPO.getMMRAUSER());
                mmrb_tPO.setMMRBGRUP(mmra_tPO.getMMRAGRUP());
                mmrb_tPO.setMMRBORIU(mmra_tPO.getMMRAORIU());
                mmrb_tPO.setMMRBORIG(mmra_tPO.getMMRAORIG());
                mmrb_tPO.setMMRBMODU(mmra_tPO.getMMRAMODU());
                mmrb_tPO.insert();
            }
        }
        //返回部分
        outDto.put("GWCS010", inDto.getString("GWCS010"));
        outDto.put("GWCS008", inDto.getString("GWCS008"));
        outDto.put("GWCS001", inDto.getString("GWCS001"));
        outDto.put("GWCS019", inDto.getString("GWCS019"));
        return outDto;
    }

    /**
     * 取消回报
     *
     * @param
     */
    @Transactional
    public Dto reconfirm(Dto inDto) {
        Dto outDto = new Dto();
        //设备安装信息表部分
        GwcvT gwcvT = new GwcvT();
        QueryWrapper<GwcvT> gwcvTQueryWrapper = new QueryWrapper<>();
        gwcvTQueryWrapper.eq("GWCVENT",inDto.getString("GWCSENT")).
                eq("GWCVDOCNO",inDto.getString("GWCVDOCNO"));
        gwcvT.delete(gwcvTQueryWrapper);
        //客户已购设备表部分
        List<Dto> gwcuList = gwat600Mapper.getGwcu(inDto);
        if (gwcuList.size() == 0) {
            QueryWrapper<MmraT> mmraTQueryWrapper = new QueryWrapper<>();
            mmraTQueryWrapper.eq("MMRAENT",inDto.getString("GWCSENT")).
                    eq("MMRA001",inDto.getString("GWCS010")).
                    eq("MMRA002",inDto.getString("GWCS001")).
                    eq("MMRA003",inDto.getString("GWCS019"));
            new MmraT().delete(mmraTQueryWrapper);
            QueryWrapper<MmrbT> mmrbTQueryWrapper = new QueryWrapper<>();
            mmrbTQueryWrapper.eq("MMRBENT",inDto.getString("GWCSENT")).
                    eq("MMRB001",inDto.getString("GWCS010")).
                    eq("MMRB002",inDto.getString("GWCS001")).
                    eq("MMRB003",inDto.getString("GWCS019"));
            new MmrbT().delete(mmrbTQueryWrapper);
        }
        //返回部分
        outDto.put("GWCVDOCNO", inDto.getString("GWCVDOCNO"));
        return outDto;
    }

    /**
     * 扫码增加商品
     *
     * @param inDto
     */
    public Dto addGood(Dto inDto) {
        inDto.put("LANG", "2");
        List<Dto> goods = gwat600Mapper.getGwcvDetail(inDto);
        Dto good = new Dto();
        if (goods!=null && goods.size()>0)
            good = goods.get(0);
        return good;
    }

    /**
     * 检查是否回报
     *
     * @param inDto
     */
    public String validate(JSONObject inDto) {
        if ("single".equals(inDto.getString("type"))) {
            inDto = JSON.parseObject(inDto.getString("item"));

        }
        if (ToolUtil.isEmpty(inDto.getString("GWCV006"))) {
            return inDto.getString("GWAKL004") + "(" + inDto.getString("GWCS008") + ")" + "安装人员为空，请检查";
        } else if (ToolUtil.isEmpty(inDto.getString("GWCV009"))) {
            return inDto.getString("GWAKL004") + "(" + inDto.getString("GWCS008") + ")" + "安装日期为空，请检查";
        }
        return null;
    }

    /**
     * 检查是否可取消回报
     *
     * @param inDto
     */
    public String check(Dto inDto) {
        List<Dto> checklist = gwat310Mapper.getGwc(inDto);
        {
            if (checklist.size() > 0) {
                return inDto.getString("GWAKL004") + "(" + inDto.getString("GWCS008") + ")" + "有后续单据,不可操作";
            }
        }
        return null;
    }

    /**
     * 获取客户所在区域的业务人员,经销商负责人,线人
     *
     * @param inDto
     * @return
     */
//    public List<Dto> getGwaa(Dto inDto) {
//        inDto.put("GWAAENT", "80");
//        return gwat600Mapper.getGwaa(inDto);
//    }
}
